try:
    import shelve
    fname = 'non_existent.tmp.dat'
    f = open(fname, 'rb')
    d = shelve.open(fname)
    print(d['a'])
except KeyError as ex:
    print(f'{ex!r}')
except FileNotFoundError as ex:
    print(f'{ex!r}')
except NameError as ex:
    print(f'{ex!r}')
else:
    print('All are good!')
finally:
    try:
        if d is not None:
            d.close()
    except:
        pass
    try:
        if f is not None:
            f.close()
    except:
        pass
